@extends('admin.layouts.app')

@section('title', '系统设置')

@section('content')
<div class="container-fluid">
    <!-- 页面标题 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0 text-gray-800">系统设置</h1>
            <p class="mb-0 text-muted">管理系统的各项配置参数</p>
        </div>
        <div>
            <a href="{{ route('admin.system-settings.create') }}" class="btn btn-primary btn-sm me-2">
                <i class="fas fa-plus"></i> 新增设置
            </a>
            <a href="{{ route('admin.system-settings.export') }}" class="btn btn-outline-secondary btn-sm me-2">
                <i class="fas fa-download"></i> 导出
            </a>
            <button type="button" class="btn btn-outline-info btn-sm me-2" data-bs-toggle="modal" data-bs-target="#importModal">
                <i class="fas fa-upload"></i> 导入
            </button>
            <form action="{{ route('admin.system-settings.clear-cache') }}" method="POST" style="display: inline;">
                @csrf
                <button type="submit" class="btn btn-outline-warning btn-sm" 
                        onclick="return confirm('确定要清除缓存吗？')">
                    <i class="fas fa-trash"></i> 清除缓存
                </button>
            </form>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="row">
        <!-- 左侧分组导航 -->
        <div class="col-md-3">
            <div class="card">
                <div class="card-header">
                    <h6 class="card-title mb-0">设置分组</h6>
                </div>
                <div class="list-group list-group-flush">
                    @foreach($groups as $key => $name)
                        <a href="{{ route('admin.system-settings.index', ['group' => $key]) }}" 
                           class="list-group-item list-group-item-action {{ $group === $key ? 'active' : '' }}">
                            <i class="fas fa-{{ $key === 'general' ? 'cog' : ($key === 'email' ? 'envelope' : ($key === 'security' ? 'shield-alt' : ($key === 'interface' ? 'palette' : ($key === 'upload' ? 'cloud-upload-alt' : 'server')))) }} me-2"></i>
                            {{ $name }}
                            <span class="badge bg-secondary float-end">{{ $settings->where('group', $key)->count() }}</span>
                        </a>
                    @endforeach
                </div>
            </div>
        </div>

        <!-- 右侧设置内容 -->
        <div class="col-md-9">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="card-title mb-0">{{ $groups[$group] ?? '设置列表' }}</h6>
                    <small class="text-muted">共 {{ $settings->count() }} 项设置</small>
                </div>
                <div class="card-body">
                    @if($settings->count() > 0)
                        <form action="{{ route('admin.system-settings.batch-update') }}" method="POST" id="settingsForm">
                            @csrf
                            <div class="row">
                                @foreach($settings as $setting)
                                    <div class="col-md-6 mb-4">
                                        <div class="border rounded p-3 h-100">
                                            <div class="d-flex justify-content-between align-items-start mb-2">
                                                <div>
                                                    <label class="form-label fw-bold mb-1">{{ $setting->label }}</label>
                                                    @if($setting->is_required)
                                                        <span class="text-danger">*</span>
                                                    @endif
                                                    @if($setting->is_public)
                                                        <span class="badge bg-info ms-1">公开</span>
                                                    @endif
                                                </div>
                                                <div class="dropdown">
                                                    <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                                        <i class="fas fa-ellipsis-v"></i>
                                                    </button>
                                                    <ul class="dropdown-menu">
                                                        <li><a class="dropdown-item" href="{{ route('admin.system-settings.show', $setting) }}"><i class="fas fa-eye me-2"></i>查看</a></li>
                                                        <li><a class="dropdown-item" href="{{ route('admin.system-settings.edit', $setting) }}"><i class="fas fa-edit me-2"></i>编辑</a></li>
                                                        <li><hr class="dropdown-divider"></li>
                                                        <li>
                                                            <button type="button" class="dropdown-item text-danger" onclick="deleteSetting({{ $setting->id }})">
                                                                <i class="fas fa-trash me-2"></i>删除
                                                            </button>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                            
                                            @if($setting->description)
                                                <p class="text-muted small mb-2">{{ $setting->description }}</p>
                                            @endif
                                            
                                            <div class="mb-2">
                                                @if($setting->type === 'boolean')
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="settings[{{ $setting->key }}]" 
                                                               id="setting_{{ $setting->id }}" value="1" 
                                                               {{ $setting->value ? 'checked' : '' }}>
                                                        <label class="form-check-label" for="setting_{{ $setting->id }}">
                                                            {{ $setting->value ? '启用' : '禁用' }}
                                                        </label>
                                                    </div>
                                                @elseif($setting->type === 'textarea')
                                                    <textarea class="form-control" name="settings[{{ $setting->key }}]" rows="3" 
                                                              placeholder="请输入{{ $setting->label }}" 
                                                              {{ $setting->is_required ? 'required' : '' }}>{{ $setting->value }}</textarea>
                                                @elseif($setting->options && is_array($setting->options))
                                                    <select class="form-select" name="settings[{{ $setting->key }}]" 
                                                            {{ $setting->is_required ? 'required' : '' }}>
                                                        @if(!$setting->is_required)
                                                            <option value="">请选择</option>
                                                        @endif
                                                        @foreach($setting->options as $optionKey => $optionValue)
                                                            <option value="{{ $optionKey }}" {{ $setting->value == $optionKey ? 'selected' : '' }}>
                                                                {{ $optionValue }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                @else
                                                    <input type="{{ $setting->type === 'integer' ? 'number' : ($setting->type === 'email' ? 'email' : 'text') }}" 
                                                           class="form-control" name="settings[{{ $setting->key }}]" 
                                                           value="{{ $setting->value }}" 
                                                           placeholder="请输入{{ $setting->label }}" 
                                                           {{ $setting->is_required ? 'required' : '' }}>
                                                @endif
                                            </div>
                                            
                                            <div class="d-flex justify-content-between align-items-center">
                                                <small class="text-muted">
                                                    键名: <code>{{ $setting->key }}</code>
                                                </small>
                                                <small class="text-muted">
                                                    类型: {{ $setting->type }}
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                            
                            <div class="text-end mt-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save"></i> 保存设置
                                </button>
                            </div>
                        </form>
                    @else
                        <div class="text-center py-5">
                            <i class="fas fa-cog fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无设置项</h5>
                            <p class="text-muted">该分组下还没有任何设置项</p>
                            <a href="{{ route('admin.system-settings.create') }}" class="btn btn-primary">
                                <i class="fas fa-plus"></i> 创建第一个设置
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 导入模态框 -->
<div class="modal fade" id="importModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">导入设置</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('admin.system-settings.import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="importFile" class="form-label">选择文件</label>
                        <input type="file" class="form-control" id="importFile" name="file" accept=".json" required>
                        <div class="form-text">请选择JSON格式的设置文件</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">导入</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

<!-- 隐藏的删除表单 -->
<form id="deleteForm" method="POST" style="display: none;">
    @csrf
    @method('DELETE')
</form>

@push('scripts')
<script>
// 开关切换时更新标签
document.querySelectorAll('input[type="checkbox"]').forEach(function(checkbox) {
    checkbox.addEventListener('change', function() {
        const label = this.nextElementSibling;
        label.textContent = this.checked ? '启用' : '禁用';
    });
});

// 表单提交处理
document.getElementById('settingsForm').addEventListener('submit', function(e) {
    const submitBtn = this.querySelector('button[type="submit"]');
    
    // 防止重复提交
    if (submitBtn.disabled) {
        e.preventDefault();
        return false;
    }
    
    // 显示加载状态
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 保存中...';
    
    // 允许表单正常提交
    return true;
});

// 删除设置功能
function deleteSetting(settingId) {
    if (confirm('确定要删除这个设置吗？')) {
        const deleteForm = document.getElementById('deleteForm');
        deleteForm.action = '/admin/system-settings/' + settingId;
        deleteForm.submit();
    }
}
</script>
@endpush